package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.ChatSession;
import com.sywb.chuangyebao.bean.NewsOwner;
import com.sywb.chuangyebao.contract.UserChatContract;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.dialog.ReportOrShieldDialog;

import org.bining.footstone.db.DbManager;
import org.bining.footstone.db.assit.WhereBuilder;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;
import org.bining.footstone.utils.ToastUtils;
import org.bining.footstone.widget.PullToRefreshView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 用户私信聊天
 */
public class UserChatActivity extends ActionbarActivity<UserChatContract.Presenter> implements UserChatContract.View {

    @BindView(R.id.chat_input)
    LinearLayout chatInput;
    @BindView(R.id.chat_et_input)
    EditText chatEtInput;
    @BindView(R.id.chat_tv_input)
    TextView chatTvInput;

    @BindView(R.id.common_recycler)
    RecyclerView commonRecycler;
    @BindView(R.id.common_refresh)
    PullToRefreshView commonRefresh;

    @BindView(R.id.common_nodata)
    RelativeLayout commonNodata;
    @BindView(R.id.common_nodata_content)
    TextView commonNodataContent;
    @BindView(R.id.common_nodata_icon)
    ImageView commonNodataIcon;
    @BindView(R.id.common_nodata_subtitle)
    TextView commonNodataSubtitle;

    private int uid;
    private ChatSession chatSession;
    private NewsOwner chatOwner;

    @Override
    public int getLayoutId(Bundle savedInstanceState) {
        return R.layout.activity_user_chat;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        if (savedInstanceState == null) {
            uid = getIntent().getIntExtra("p0", 0);
        } else {
            uid = savedInstanceState.getInt("p0", 0);
        }

        chatSession = DbManager.getInstance().queryById(uid, ChatSession.class);
        if (chatSession == null) {
            ToastUtils.show(mContext, "初始化会话信息错误,请稍后再试");
            return;
        }
        chatOwner = chatSession.getOwner();

        setTitle(TextUtils.isEmpty(chatOwner.nickname) ? getString(R.string.app_name) : chatOwner.nickname);

        commonRecycler.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorLight));

        addMenuImageItme(R.drawable.news_more_normal, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ReportOrShieldDialog dialog = ReportOrShieldDialog.newInstance(uid);
                dialog.setOnItemListener(new ReportOrShieldDialog.OnItemListener() {
                    @Override
                    public void onResult(int postion, boolean isSuccess, String message) {
                        ToastUtils.show(mContext, message);
                        if (postion == 2 && isSuccess) {
                            DbManager.getInstance().delete(new WhereBuilder(ChatSession.class).where("session_uid = ?", String.valueOf(chatOwner.uid)));
                            exit();
                        }
                    }
                });
                dialog.show(getMyFragmentManager(), "Chat");
            }
        });
    }

    @OnClick({R.id.chat_tv_input})
    public void onClick(View view) {
        if (isCanClick(view)) {
            switch (view.getId()) {
                case R.id.chat_tv_input:
                    if (!Utils.checkIsLogin()) {
                        return;
                    }

                    String message = chatEtInput.getText().toString();
                    if (TextUtils.isEmpty(message)) {
                        ToastUtils.show(mContext, "请输入内容！");
                        return;
                    }
                    mPresenter.sendMessage(message);

                    chatSession.msg = message;
                    DbManager.getInstance().save(chatSession);

                    chatEtInput.setText(null);
                    break;
            }
        }
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_CHAT_MESSAGE)})
    public void rxChatReceiver(String data) {
        mPresenter.onStartRefresh();
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

    @Override
    public PullToRefreshView getPullToRefreshView() {
        return commonRefresh;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return commonRecycler;
    }

    @Override
    public void setNoData(boolean isNoData) {
        commonNodata.setVisibility(isNoData ? View.VISIBLE : View.GONE);
        commonNodataContent.setText("暂无历史聊天记录");
    }

    @Override
    public NewsOwner getChatOwner() {
        return chatOwner;
    }

}
